<%@page import="java.util.ArrayList"%>
<%@page import="entity.Area"%>
<%@page import="java.util.List"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.DriverManager"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>

<%
	String _index = request.getParameter("index"); // 页码	
	String _pageSize = request.getParameter("pageSize"); // 容量
	
	if(_index == null || _pageSize == null){
		_index = "1";
		_pageSize = "10";
	}

	
	int index = Integer.parseInt(_index);
	int pageSize = Integer.parseInt(_pageSize);
	if(index<1){
		index=1;
	}
	

	// select * from area limit 0,10

	Class.forName("com.mysql.jdbc.Driver");
	Connection conn =  DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/tb108","root","root");
	PreparedStatement ps =  conn.prepareStatement(" select * from area limit ?,?");
	int start = (index - 1) * pageSize ;
	ps.setInt(1, start);
	ps.setInt(2, pageSize);
	
	ResultSet rs =  ps.executeQuery();
	List<Area> list = new ArrayList<Area>();
	while(rs.next()){
		Area a = new Area();
		a.setId(rs.getInt("id"));
		a.setName(rs.getString("name"));
		list.add(a);
	}
	
	ps = conn.prepareStatement("select count(1) from area");
	ResultSet rs1 =  ps.executeQuery();
	int total = 0;
	if(rs1.next()){
		total = rs1.getInt(1);
	}
	
	rs.close();
	ps.close();
	conn.close();
	
	int pageTotal = total % pageSize == 0 ? (total / pageSize):(total / pageSize +1);
	
	
%>


<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
	table {
		width: 600px;
		border: 1px solid #aaa;
		text-align: center;
		margin: 30px auto;
	}
</style>
</head>
<body>
	<table>
		<tr>
			<th>ID</th>
			<th>NAME</th>
		</tr>
		
		<%
			for(Area a :list){
		%>
		<tr>
			<td>
				<%= a.getId() %>
			</td>
			<td>
				<%= a.getName() %>
			</td>
		</tr>
		<%
			}
		%>
		<tr>
			<td colspan="2">
				共<%=pageTotal %>页 第<%=index %>页 共<%=total %>条
			</td>
		</tr>
		<tr>
			<td colspan="2">
				<a href="/demo/index.jsp?pageSize=<%= pageSize %>&index=1">首页</a>
				
				<% if(index > 1){ %>
				<a href="/demo/index.jsp?pageSize=<%= pageSize %>&index=<%= index-1 %>">上一页</a>
				<%} %>
				
				<% if(index < pageTotal){ %>
				<a href="/demo/index.jsp?pageSize=<%= pageSize %>&index=<%= index+1 %>">下一页</a>
				<%} %>
				<a href="/demo/index.jsp?pageSize=<%= pageSize %>&index=<%= pageTotal %>">尾页</a>
			</td>
		</tr>
		
	</table>

</body>
</html>